//结算方管理

/**
 * 添加/编辑视图模型
 */
interface ISettleManagentDetailViewModel {
    /**
     * 结算方编号
     */
    id?: string;
    /**
     * 单位名称
     */
    nameBox: string;
    /**
     * 联系人
     */
    contactManBox: string;
    /**
     * 联系电话
     */
    phoneNumber: string;
    /**
     * 是否启用
     */
    isEnabled?:string;
    /**
     * 备注
     */
    remarks: string;
    /**
     * 保存事件
     */
    settleSave: () => void;
}

/**
 * 结算方管理视图模型
 */
interface SettleManagentViewModel extends IBaseViewModel<ISettleManagentQueryTerms> {
    /**
    * ui-grid控制
    */
    gridOptions: uiGrid.IGridOptionsOf<SettleListItemResponse>;
    /**
    * 查询事件
    */
    query: () => void;
    /**
     * 结算方添加/编辑视图模型
     */
    detail: ISettleManagentDetailViewModel;
    /**
     *客户单位
     */
    clientName: string;
    /**
     * 标题
     */
    title: string;
    /**
     * 添加结算方
     */
    addSettle: () => void;
    /**
     * 编辑结算方
     */
    editSettle: (id: string) => void;
    /**
     * 删除结算方
     */
    deleteSettle: (id: string) => void;
    /**
     * 列表高
     */
    heightA:number;
}

/**
 * 查询条件
 */
interface ISettleManagentQueryTerms {
    /**
     * 结算单位名称
     */
    name: string;
    /**
     * 联系人
     */
    contactMan: string;
}

class SettleManagentController extends ListController<SettleManagentViewModel, ISettleManagentQueryTerms>{
    gridApi: uiGrid.IGridApiOf<SettleListItemResponse>;
    enterPriseId: string;
    isEnabled: boolean;

    constructor(public $scope: SettleManagentViewModel, private settleService: ISettleService, private $ngBootbox: BootboxService,private $location: ng.ILocationService ) {
        super($scope);
        this.init();
    }

    init(): void {
        super.init();

        this.enterPriseId = JSON.parse(window.localStorage.getItem("client").toString()).clientId;
        this.$scope.clientName = JSON.parse(window.localStorage.getItem("client").toString()).clientName;


        this.isEnabled = null;

        this.$scope.addSettle = this.showAddSettleDialog;
        this.$scope.editSettle = this.showEditSettleDialog;
        this.$scope.deleteSettle = this.showDeleteSettleDialog;

        this.$scope.queryTerms = {
            name: "",
            contactMan: "",
        };

        this.$scope.gridOptions = {
            enableSorting: false,
            paginationPageSizes: [10, 20, 30],
            useExternalPagination: true,
            useExternalSorting: true,
            enableColumnResizing: true,
            columnDefs: [
                { name: "序号", field: "index", enableColumnMenu: false },
                { name: "单位名称", field: "name", enableColumnMenu: false },
                { name: "联系人", field: "contactMan", enableColumnMenu: false },
                { name: "添加时间", field: "createTime", enableColumnMenu: false },
                {
                    name: "操作",
                    cellFilter: 'checkmark',
                    cellTemplate: '<a data-toggle="modal" title="编辑" data-target="#settle" class="fa fa-edit m-l-xs primary text-info" ng-click="grid.appScope.editSettle(row.entity.id)"></a> <a class="primary fa fa-trash-o text-danger" title="删除" ng-click="grid.appScope.deleteSettle(row.entity.id)"></a>', enableColumnMenu: false
                }],
            onRegisterApi: (gridApi) => {
                this.gridApi = gridApi;
                gridApi.pagination.on.paginationChanged(this.$scope, this.paginationChanged);
            }
        };
        this.$scope.query = this.query;
        this.query();
    }

    loadData(queryTerms: ISettleManagentQueryTerms, skip: number, count: number): void {
        this.settleService.getList(this.enterPriseId, queryTerms.name, queryTerms.contactMan, this.isEnabled, skip, count).then(result => {
            this.$scope.gridOptions.totalItems = result.data.total;
            result.data.data.map((item, index) => item.index = skip++ +1);
            this.$scope.gridOptions.data = result.data.data;

            this.$scope.heightA = (this.$scope.gridOptions.data.length * 39 )+ 95;

            if (skip == 0) {
                this.$scope.gridOptions.paginationCurrentPage = 1;
            }
            this.$scope.querying = false;
        }, error => {
            this.$scope.querying = false;
        });
    }

    /**
     * 显示添加结算方窗口
     */
    showAddSettleDialog = () => {
        this.$scope.title = "添加结算方";
        this.$scope.detail = {
            nameBox: "",
            contactManBox: "",
            phoneNumber: "",
            isEnabled: "true",
            remarks: "",
            settleSave: this.addSettle
        };
    }

    addSettle = () => {
        if (!$("#settleForm").valid()) { return };
        this.$ngBootbox.confirm("确定保存该数据吗？").then(e => {
            if (this.isRadioResponse(this.$scope.detail.isEnabled)) {
                this.settleService.add(
                    this.enterPriseId,  
                    this.$scope.detail.nameBox,
                    this.$scope.detail.contactManBox,
                    this.$scope.detail.phoneNumber,
                    this.$scope.detail.isEnabled,
                    this.$scope.detail.remarks
                ).then(result => {
                    this.reload();
                    $('#settle').modal('hide');
                });
            }
        })
    }

    /**
     * 显示编辑结算方窗口
     */
    showEditSettleDialog = (id: string) => {
        this.$scope.title = "编辑结算方";
        var validator = $('#projectForm').validate();
        // validator.prepareForm();
        //validator.hideErrors();
        $(".error").removeClass("error");
        this.settleService.getDetail(id).then(result => {
            this.$scope.detail = {
                id: id,
                nameBox: result.data.name,
                contactManBox: result.data.contactMan,
                phoneNumber: result.data.phoneNumber,
                isEnabled: String(result.data.isEnable),
                remarks: result.data.remarks,
                settleSave: this.editSettle
            }
        })
    }

    isRadioResponse(response: string | boolean): response is boolean {
        return (<boolean>response) !== undefined;
    }

    editSettle = () => {
        this.$ngBootbox.confirm("确定保存该数据吗？").then(e => {
            if (this.isRadioResponse(this.$scope.detail.isEnabled)) {
                this.settleService.edit(
                    this.$scope.detail.id,
                    this.enterPriseId,
                    this.$scope.detail.nameBox,
                    this.$scope.detail.contactManBox,
                    this.$scope.detail.phoneNumber,
                    this.$scope.detail.isEnabled,
                    this.$scope.detail.remarks
                ).then(result => {
                    this.reload();
                    $('#settle').modal('hide');
                })
            }
        })
    }
    showDeleteSettleDialog = (id: string) => {
        this.$ngBootbox.confirm("是否删除该数据？").then(e => {
            this.settleService.delete(id).then(result => {
                this.reload();
            })
        })
    }
    
}

angular.module("tms").controller("settleManagentController", ["$scope", "settleService", "$ngBootbox","$location", SettleManagentController])